﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using DataSync.DAL;
using DataSync.BLL;

namespace DataSync.BLL
{
    public class SyncLog
    {
        private string tableName;

        private DateTime? startTime = DateTime.Now;
        private DateTime? endTime = null;

        private DateTime? maxTime = null;
        private int? maxID = null;

        private long? rows = null;
        private bool finished = false;

        public SyncLog()
        { }

        /// <summary>
        /// 目标数据库中数据表名称
        /// </summary>
        public string TableName
        {
            get { return tableName; }
            set { tableName = value; }
        }

        /// <summary>
        /// 开始同步时间，默认为系统当前时间
        /// </summary>
        public DateTime? StartTime
        {
            get { return startTime; }
            set { startTime = value; }
        }

        /// <summary>
        /// 以日期增量同步时，本次同步的最大日期
        /// </summary>
        public DateTime? MaxTime
        {
            get { return maxTime; }
            set { maxTime = value; }
        }

        /// <summary>
        /// 同步完成时间
        /// </summary>
        public DateTime? EndTime
        {
            get { return endTime; }
            set { endTime = value; }
        }

        /// <summary>
        /// 以自增ID增量同步时，本次同步到的最大ID
        /// </summary>
        public int? MaxID
        {
            get { return maxID; }
            set { maxID = value; }
        }

        /// <summary>
        /// 同步的记录数
        /// </summary>
        public long? Rows
        {
            get { return rows; }
            set { rows = value; }
        }

        /// <summary>
        /// 是否正常完成
        /// </summary>
        public bool Finished
        {
            get { return finished; }
            set { finished = value; }
        }

        public int Insert(SQLHelper sqlHelper, int syncType)
        {
            SqlParameter[] para = new SqlParameter[4];

            para[0] = new SqlParameter();
            para[0].ParameterName = "@TABLE_NAME";
            para[0].SqlDbType = System.Data.SqlDbType.VarChar;
            para[0].Value = tableName;

            para[1] = new SqlParameter();
            para[1].ParameterName = "@START_TIME";
            para[1].SqlDbType = System.Data.SqlDbType.DateTime;
            para[1].Value = startTime;

            para[2] = new SqlParameter();
            para[2].ParameterName = "@MAX_TIME";
            para[2].SqlDbType = System.Data.SqlDbType.DateTime;
            para[2].Value = maxTime;

            para[3] = new SqlParameter();
            para[3].ParameterName = "@MAX_ID";
            para[3].SqlDbType = System.Data.SqlDbType.Int;
            para[3].Value = maxID;

            switch (syncType)
            {
                case 1:
                    return sqlHelper.ExecuteProc("AddDateSyncLog", para);
                case 2:
                    return sqlHelper.ExecuteProc("AddIdSyncLog", para);
                case 0:
                    return sqlHelper.ExecuteProc("AddFullDataSyncLog", para);
                default:
                    return 0;
            }
        }

        /// <summary>
        /// 同步完成后更新日志表
        /// </summary>
        /// <param name="sqlHelper"></param>
        /// <param name="syncType">同步类型，完全同步（0），按日期同步（1），按自增ID同步（2）</param>
        /// <returns></returns>
        public int Update(SQLHelper sqlHelper, int syncType)
        {
            StringBuilder updateText = new StringBuilder();
            updateText.Append("UPDATE SYNCHRONIZATION_LOGS SET ROWS=" + rows + ",END_TIME='" + maxTime + "',MAX_ID=" + maxID + ",FINISHED=1");//完成时间取系统时间
            updateText.AppendFormat(" WHERE TABLE_NAME='{0}'", tableName);

            if (syncType == 1)
            {
                updateText.AppendFormat(" AND MAX_TIME={0}", SyncManage.Object2String(maxTime));
            }
            else if (syncType == 2)
            {
                updateText.AppendFormat(" AND MAX_ID={0}", -1);
            }
            else if (syncType == 0)
            {
                updateText.AppendFormat(" AND MAX_ID=-2");
            }

            return sqlHelper.ExecuteSql(updateText.ToString());
        }

    }
}
